<?php
include('/var/www/html/new/d/module/api/function.class.php');
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
if(!isset($_REQUEST['Action'])){
	exit("200|0|0|0");
}
if($_REQUEST['Action'] == 'Relation'){
	exit("200|0|0|0");
}elseif($_REQUEST['Action'] == "MOD"){//修改 and 修改

	logfile(json_encode($_REQUEST),'api_cancel');
	if($_REQUEST['Ordern'] == '2416725') exit("200");
	$PFT_infunction = new AllFunction();
	$Ordern = $_REQUEST['Ordern'];
	$Ordern_xml = $PFT_infunction->Select_PFTorder($Ordern); 
	$UUlid = (int)$Ordern_xml->Rec->UUlid; //景区id
	$UUtid = (int)$Ordern_xml->Rec->UUtid;//门票id
	$UUpid = (int)$Ordern_xml->Rec->UUpid; //产品id
	$UUremotenums = explode("_",(string)$Ordern_xml->Rec->UUremotenum);	
	$UUremotenum = $UUremotenums[1]; //远端订单号
	$UUtnum = (int)$Ordern_xml->Rec->UUtnum;	//订单数量
	$UUctime = (string)$Ordern_xml->Rec->UUctime;//取消时间
	$UUtprice=(string)$Ordern_xml->Rec->UUtprice; //结算价
	$UUplaytime=(string)$Ordern_xml->Rec->UUbegintime;	//游玩时间
	$IDcar =(string)$Ordern_xml->Rec->UUpersonid; //身份证
	$name =(string)$Ordern_xml->Rec->UUordername;//订单客人姓名
	$ordertel=(string)$Ordern_xml->Rec->UUordertel;//手机号
	$UUlprice=$PFT_infunction->Get_price($UUpid,$UUplaytime,1);//零售价
	$UUaids=(string)$Ordern_xml->Rec->UUaids;
	if($UUaids == 0){
		$buyid = (string)$Ordern_xml->Rec->UUbuyid;
	}else{
		$UUaidss= explode(',',$UUaids);
		$buyid = $UUaidss[1];
	}
	$select_gprice = "select dprice from uu_priceset where tid=".$UUpid." and pid=".$buyid." limit 1";
	$GLOBALS['le']->query($select_gprice);
	$row = $GLOBALS['le']->fetch_assoc();
	$dprice = $row['dprice'];  
	$gprice = $UUtprice - $dprice; //查询供应商的供货价

	$select_uuid = "select uuid from uu_jq_ticket where id=".$UUtid." limit 1";
	$GLOBALS['le']->query($select_uuid);
	$row1 = $GLOBALS['le']->fetch_assoc();
	$uuid = $row1['uuid'];  //查询商品编码id

	if($_REQUEST['Tnum'] == 0){ //取消
		$rnum = $UUtnum;
		$rectime = $UUctime;
	}else{ //修改
		$rnum = $UUtnum - $_REQUEST['Tnum']; //退款数量
		$rectime = date('Y-m-d H:i:s');
	}	
	$modify_array = array(
		'PFTOrdern'=>$Ordern,
		'APIOrdern'=>$UUremotenum,
		'tnum'=>$UUtnum,
		'rnum'=>$rnum,
		'tprice'=>$UUtprice,
		'lprice'=>$UUlprice,
		'gprice'=>$gprice,
		'rectime'=>$rectime,
		'idcard'=>$IDcar,
		'name'=>$name,
		'uuid'=>$uuid,
		'ordertel'=>$ordertel
	);	
	switch($UUlid){ 
		case 5322: //鼓浪屿（九天）
			if($_REQUEST['Tnum'] == 0){
				$result = file_get_contents("http://max.12301.cc/gate_machine_order_cancel.php?ordern=".$Ordern);
			}else{
				$result = file_get_contents("http://max.12301.cc/gate_machine_order_cancel.php?ordern=".$Ordern."&tnum=".$_REQUEST['Tnum']."&pirce=".$UUtprice);
			}
			if($result == 101) exit("1077|0|0|0");
			else { 
				$str = "update uu_order_fx_details set memo='gly_modify_success' where orderid=".$Ordern." limit 1";
				$GLOBALS['le']->query($str);
				exit("200|0|0|0");
			}
		break;

		case 8048:
		case 8055:
		case 8054:
			if($_REQUEST['Tnum'] == 0){//取消订单
				include_once('RuiJin/ruijin.class.php');
				$RJ = new RunJin($UUlid);  //九天-瑞金		
				$result = $RJ->removeOrder($Ordern);
				if($result == 101) exit("1077|0|0|0");
				else{
					$str = "update uu_order_fx_details set memo='100_3' where orderid=".$Ordern." limit 1";
					$GLOBALS['le']->query($str);
					exit("200|0|0|0");
				}
			}
		break;
		
		case 10180:
		case 10179:
		case 10178:
		case 10185:
		case 10234:
		case 10251:
		case 9904:
		case 9909:
		case 9911:
		case 10292:
		case 10293:
			include_once 'ShanHaiGuan/shanhaiguan.class.php';
			$shg = new shanhaiguan($UUlid); //山海关
			$modify_array['orderMoney']= $UUtnum * $UUlprice /100;
			$result = $shg->applyOrderRefundByUser($modify_array);
			if($result == 101) exit("1077|0|0|0");
			else{
				$str = "update uu_order_fx_details set memo='shg_SUCCESS' where orderid=".$Ordern." limit 1";
				$GLOBALS['le']->query($str);
				exit("200|0|0|0");
			}
		break;
		
		/* case 10414:
			include_once 'FuRongYuan/furongyuan.class.php';
			$fry = new FuRongYuan($UUlid); //芙蓉园
			$result = $fry->RefundOrder($modify_array);
			if($result == 101) exit("1077|0|0|0");
			else{
				$str = "update uu_order_fx_details set memo='ysh_SUCCESS' where orderid=".$Ordern." limit 1";
				$GLOBALS['le']->query($str);
				exit("200|0|0|0");
			}
		break;
		
		case 9415:
		case 8533:
			include_once 'YanSaiHu/yansaihu.class.php';
			$ysh = new YanSaiHu($UUlid); //燕塞湖
			$result = $ysh->oCancelOrder($modify_array);
			if($result == 101) exit("1077|0|0|0");
			else{ //修改成功
				$str = "update uu_order_fx_details set memo='ysh_SUCCESS' where orderid=".$Ordern." limit 1";
				$GLOBALS['le']->query($str);
				exit("200|0|0|0");
			}
		break; */
		
		default:
		break;
	}
}
function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/api_cancel/'.$path.'.txt';
	$word = date("Y-m-d H:i:s")."|".$word;
	if(file_exists($filepath)){
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
	}
	$fp = fopen($filepath,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,$word."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>
